package backup;
//package cs6238.common;
//
//import java.io.FileInputStream;
//import java.io.InputStream;
//import java.security.InvalidKeyException;
//import java.security.NoSuchAlgorithmException;
//import java.security.NoSuchProviderException;
//import java.security.Principal;
//import java.security.SignatureException;
//import java.security.cert.CertificateException;
//import java.security.cert.CertificateFactory;
//import java.security.cert.X509Certificate;
//
//import javax.net.ssl.X509TrustManager;
//
///**
// * This is used for both the client and server when establishing SSL connections
// */
//public class CustomTrustManager implements X509TrustManager {
//	
//	private String _caCertFilePath;
//	
//	public CustomTrustManager(String caCertFilePath){
//		_caCertFilePath = caCertFilePath;
//	}
//	
//	@Override
//	public void checkClientTrusted(X509Certificate[] chain, String authType)
//			throws CertificateException {
//
//		System.out.println("Oh yeah? " + authType);
//		
//		try{
//			for(X509Certificate cert : chain){
//				checkCertificateTrusted(cert);
//			}
//		}catch(Exception e){
//			new CertificateException(e);
//		}
//	}
//
//	@Override
//	public void checkServerTrusted(X509Certificate[] chain, String authType)
//			throws CertificateException {
//		
//		System.out.println("Oh yeah!!!!!!!! " + authType);
//		
//		try{
//			for(X509Certificate cert : chain){
//				checkCertificateTrusted(cert);
//			}
//		}catch(Exception e){
//			new CertificateException(e);
//		}
//	}
//	
//	/**
//	 * Check that the cert is valid, and it is signed by our CA 
//	 * @param cert
//	 * @throws CertificateException
//	 * @throws SignatureException 
//	 * @throws NoSuchProviderException 
//	 * @throws NoSuchAlgorithmException 
//	 * @throws InvalidKeyException 
//	 */
//	private void checkCertificateTrusted(X509Certificate cert) throws CertificateException, InvalidKeyException, NoSuchAlgorithmException, NoSuchProviderException, SignatureException{
//		cert.checkValidity();
//		X509Certificate caCert = readCACertificate();
//		cert.verify(caCert.getPublicKey());
//	}
//
//	/**
//	 * Only accept connections that have been signed by our CA
//	 */
//	@Override
//	public X509Certificate[] getAcceptedIssuers() {
//		return new X509Certificate[]{ readCACertificate() };
//	}
//	
//	private X509Certificate readCACertificate(){
//		try{
//			InputStream inStream = new FileInputStream(_caCertFilePath);
//			CertificateFactory cf = CertificateFactory.getInstance("X.509");
//			X509Certificate cert = (X509Certificate) cf.generateCertificate(inStream);
//			inStream.close();
//			return cert;
//		}catch(Exception e){
//			e.printStackTrace();
//		}
//		
//		return null;
//	}
//
//}
